﻿// owner 蒋家伟

#pragma once


// 使用方法 ：DEFINE_DRIVER_BASE, DEFINE_DRIVER_FROM, DEFINE_DRIVER_FROM2 定义继承体系，暂时支持到两个继承体系
// 其他宏无需使用
// 如果提示get_XXX_tag_address等错误，请查看是否存在继承断开的定义

// 此宏不要直接使用，请使用下面的定义
#define CAST_TAG_DEFINE(CLASSNAME) \
protected:\
static const char* get_##CLASSNAME##_tag_address()\
{\
    static const char* s_##CLASSNAME##_castTag = typeid(CLASSNAME).name(); \
    return s_##CLASSNAME##_castTag;\
}

#define CAST_IMP_DEFINE(CLASSNAME)\
public:\
virtual bool cast_imp(const char* pTargetaddress, uint64_t& this_address) const\
{\
    return CLASSNAME::cast_##CLASSNAME##imp(pTargetaddress, this_address);\
}

// 在类内以下此宏
// 如果认为当前类为基类，则可以用DEFINE_DRIVER_BASE定义
#define DEFINE_DRIVER_BASE(CLASSNAME) \
CAST_TAG_DEFINE(CLASSNAME) \
bool cast_##CLASSNAME##imp(const char* pTargetaddress, uint64_t& this_address) const\
{\
    if(CLASSNAME::get_##CLASSNAME##_tag_address() == pTargetaddress)\
    {\
        this_address = reinterpret_cast<uint64_t>(this);\
        return true;\
    }\
    return false;\
}\
CAST_IMP_DEFINE(CLASSNAME)

// 如果认为当前类为子类，且派生于某一个类别，则用此宏定义
#define DEFINE_DRIVER_FROM(CLASSNAME, BASECLASSNAME) \
CAST_TAG_DEFINE(CLASSNAME) \
bool cast_##CLASSNAME##imp(const char* pTargetaddress, uint64_t& this_address) const\
{\
    if(CLASSNAME::get_##CLASSNAME##_tag_address() == pTargetaddress)\
    {\
        this_address = reinterpret_cast<uint64_t>(this);\
        return true;\
    }\
    else\
    {\
        return BASECLASSNAME::cast_##BASECLASSNAME##imp(pTargetaddress, this_address);\
    }\
}\
CAST_IMP_DEFINE(CLASSNAME)

// 如果认为当前类为子类，且派生于某两个类别，则用此宏定义
#define DEFINE_DRIVER_FROM2(CLASSNAME, BASECLASSNAME1, BASECLASSNAME2) \
CAST_TAG_DEFINE(CLASSNAME) \
bool cast_##CLASSNAME##imp(const char* pTargetaddress, uint64_t& this_address) const\
{\
    if(CLASSNAME::get_##CLASSNAME##_tag_address() == pTargetaddress)\
    {\
        this_address = reinterpret_cast<uint64_t>(this);\
        return true;\
    }\
    else if(BASECLASSNAME1::cast_##BASECLASSNAME1##imp(pTargetaddress, this_address))\
    {\
        return true;\
    }\
    else\
    {\
        return BASECLASSNAME2::cast_##BASECLASSNAME2##imp(pTargetaddress, this_address);\
    }\
}\
CAST_IMP_DEFINE(CLASSNAME)

// 如果认为当前类为子类，且派生于某三个类别，则用此宏定义
#define DEFINE_DRIVER_FROM3(CLASSNAME, BASECLASSNAME1, BASECLASSNAME2, BASECLASSNAME3) \
CAST_TAG_DEFINE(CLASSNAME) \
bool cast_##CLASSNAME##imp(const char* pTargetaddress, uint64_t& this_address) const\
{\
    if(CLASSNAME::get_##CLASSNAME##_tag_address() == pTargetaddress)\
    {\
        this_address = reinterpret_cast<uint64_t>(this);\
        return true;\
    }\
    else if(BASECLASSNAME1::cast_##BASECLASSNAME1##imp(pTargetaddress, this_address))\
    {\
        return true;\
    }\
    else if(BASECLASSNAME2::cast_##BASECLASSNAME2##imp(pTargetaddress, this_address))\
    {\
        return true;\
    }\
    else\
    {\
        return BASECLASSNAME3::cast_##BASECLASSNAME3##imp(pTargetaddress, this_address);\
    }\
}\
CAST_IMP_DEFINE(CLASSNAME)

